package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.qo.JsonResult;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.qo.EmployeeQueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.service.IEmployeeService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    //@RequiresPermissions(value={"员工列表","employee:list"},logical= Logical.OR)
    public String list(@ModelAttribute("qo") EmployeeQueryObject queryObject, Model model){
        PageInfo<Employee> result = employeeService.selectByQuery(queryObject);
        //List<Employee> list = employeeService.getAll();
        model.addAttribute("result",result);
        model.addAttribute("depts",departmentService.getAll());
        return "employee/list";
    }



    @RequestMapping("/input")
    //@RequiresPermissions(value={"员工添加","employee:input"},logical= Logical.OR)
    public String input(Long id, Model model){
        if(id != null){
            Employee employee = employeeService.getEmployeeById(id);
            model.addAttribute("employee",employee);
        }
        List<Department> departments = departmentService.getAll();
        model.addAttribute("depts",departments);
        List<Role> roles = roleService.getAll();
        model.addAttribute("roles",roles);
        return "employee/input";
    }
    @RequestMapping("/saveOrUpdate")
    //@RequiresPermissions(value={"员工保存或修改","employee:saveOrUpdate"},logical= Logical.OR)
    public String saveOrUpdate(Employee employee,Long[] ids){
        employeeService.saveOrUpdate(employee,ids);

        return "redirect:/employee/list.do";
    }
    @RequestMapping("/delete")
    //@RequiresPermissions(value={"员工删除","employee:delete"},logical= Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        try {
            employeeService.delete(id);
            return new JsonResult();
        }catch(Exception e){
            e.printStackTrace();
            return new JsonResult(false,"删除失败");
        }
    }
    @RequestMapping("/batchDelete")
    //@RequiresPermissions(value={"员工批量删除","employee:batchDelete"},logical= Logical.OR)
    @ResponseBody
    public JsonResult batchDelete(Long[] ids){
        try {
            employeeService.batchDelete(ids);
            return new JsonResult();
        }catch(Exception e){
            e.printStackTrace();
            return new JsonResult(false,"删除失败");
        }
    }


    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        //拿到所有员工list
        List<Employee> employees = employeeService.getAll();
        //新建httpworkbook  创建sheet  row  cell
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("员工信息");
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2).setCellValue("年龄");
        //遍历放员工
        for(int i = 0; i < employees.size();i++){
            HSSFRow rows = sheet.createRow(i + 1);
            rows.createCell(0).setCellValue(employees.get(i).getName());
            rows.createCell(1).setCellValue(employees.get(i).getEmail());
            //age自动存double类型
            rows.createCell(2).setCellValue(employees.get(i).getAge());
        }
        //输出 用response输出
        response.setHeader("content-Disposition","attachment;filename=employee.xls");
        wb.write(response.getOutputStream());
    }

    @RequestMapping("/importXls")
    public String importXls(MultipartFile file) throws IOException {
        //内容处理放service
        employeeService.importXls(file);
        return "redirect:/employee/list.do";
    }



}
